<?php
/**
 * Model
 */
class Pbz extends Psa_Model {
		
	
	/**
	 * Checks if arguments are integers and sums them
	 */
	function sum($number1, $number2){
		
		// input validation
		$validator = new Psa_Validator();
		$validator->required($number1, 'int');
		$validator->required($number2, 'int');
		
		// sum numbers
		return $number1 + $number2;
	}

	
	function read_xml($filename=false){
		if ($filename){
			$this->empty_table('transaction');
			
			$xml=simplexml_load_file($filename);
			
			$owner_id = $xml->info->bankAccount['id'];
			
			foreach ($xml->transactions->transaction as $transaction){
				$parts = explode('.', $transaction['date']);
				$transaction['date']  = "$parts[2]-$parts[1]-$parts[0]";
				$this->save_xml($transaction, $owner_id);
			}
			//prs($xml);
		} else return "No filename";
	}
	
	function empty_table($table_name){
		
		$sql= "TRUNCATE TABLE $table_name";
		$this->psa_database->query($sql);
		
	}
	
	function save_xml($transaction, $owner_id){
		
		$sql= "INSERT INTO transaction (date, pay_amount, receive_amount, description_bank, owner, currency) VALUES(?,?,?,?,?,?)";
		
		if (isset($transaction->pay['amount']) && $transaction->pay['amount'])
			$qparams = array($transaction['date'], $transaction->pay['amount'], 0, $transaction['description'], $owner_id, $transaction->pay['currency'] );
		else if (isset($transaction->receive['amount']) && $transaction->receive['amount'])
			$qparams = array($transaction['date'], 0, $transaction->receive['amount'], $transaction['description'], $owner_id, $transaction->receive['currency'] );
				
		$this->psa_database->execute($qparams,$this->psa_database->prepare($sql));
		
	}
	
	function restore_transactions($year, $month=false, $day=false, $owner_id=false){
		
		$sql= "SELECT * FROM `transaction` WHERE YEAR( date ) = $year ";
		if($month)
			$sql.="AND MONTH( date ) = $month ";
		if($day){
			$part = explode(',', $day);
			$sql.="AND DAY( date ) BETWEEN $part[0] and $part[1] ";
		}
		if($owner_id)
			$sql.="AND owner = $owner_id ";
		$sql.="ORDER BY date";
		
		
		$this->psa_database->query($sql);
		return $this->psa_database->fetch_all();
		
	}
	
	function to_int($str){
	
		$str = preg_replace("/([^0-9,-])/i", "", $str);
		return str_replace(',', '.', $str);
	}
	
	function amount_and_flag($array){
		$return['pay']=0; $return['receive']=0;$return['pay_atm']=0;$return['pay_pos']=0;$return['receive_paycheck']=0;
		$return['pay_credit_cards']=0;$return['jopa_stednja']=0;
		
			foreach($array as $item){
				$item['pay_amount']= $this->to_int($item['pay_amount']);
				$item['receive_amount']= $this->to_int($item['receive_amount']);
				$return['pay']+=$item['pay_amount'];
				$return['receive']+=$item['receive_amount'];
				
				
				//this should be put in cfg file
				if(strstr($item['description_bank'], ' ATM ')){
					$return['pay_atm']+=$item['pay_amount'];
				}
				if(strstr($item['description_bank'], 'POS ')){
					$return['pay_pos']+=$item['pay_amount'];
				}
				if(strstr($item['description_bank'], 'ISPLATA ZA ')){
					$return['receive_paycheck']+=$item['receive_amount'];
				}
				if(strstr($item['description_bank'], 'NETO PL.MUZINIC') || strstr($item['description_bank'], 'NETO PL MUŽINIĆ')){
					$return['receive_paycheck']+=$item['receive_amount'];
				}
				if(strstr($item['description_bank'], 'UPLATA - AMERICAN EXPRESS')){
					$return['pay_credit_cards']+=$item['pay_amount'];
				}
				if(strstr($item['description_bank'], 'KUPNJA ')){
					$return['jopa_stednja']+=$item['pay_amount'];
				}
				
				
			}
		$return['sum']= number_format($return['receive']-$return['pay'], 2, ',', ' ');
		
		return $return;	
	}
	
	
	
}
